/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.dataprocessing;

import simhya.model.flat.FlatModel;

/**
 *
 * @author Luca
 */
public class StochasticDataCollector extends DataCollector {
    

    public StochasticDataCollector(FlatModel model) {
        super(model);
    }


    /*
     * calcola il max numero di punti su tutte le traiettorie
     * usa il numero effettivo di punti traiettoria x traiettoria nell'aggiungere
     * gestione del final time nelle traiettorie.
     */
    public TrajectoryStatistics collectTrajectoryStatistics() {    
        int runs,points,vars;
        double[][] data;
        Trajectory t;
        boolean diffFinalTimes = false;
        boolean unevenLength = false;

        runs = super.trajectories.size();
        if (runs <= 1)
            throw new DataException("Too few trajectories to collect statistics!!!");
        t = super.trajectories.get(0);
        vars = t.dataDimension;
        points = t.savedPoints;
        for (int k=1;k<runs;k++) {
            t = super.trajectories.get(k);
            if (t.savedPoints > points) {
                //trajectories have different length
                points = t.savedPoints;
                unevenLength  = true;
            }
        }
        if (!super.saveByStep) {
            t = super.trajectories.get(0);
            double ft = t.finalState[0];
            for (int k=1;k<runs;k++) {
                t = super.trajectories.get(k);
                if (t.finalState[0] != ft)
                    diffFinalTimes = true;
            }
        }
       
        
        TrajectoryStatistics s = new TrajectoryStatistics(vars,points,runs);
        s.modelName = super.modelName;
        s.fullLatexDocument = super.fullLatexDocument;
        s.stepTrajectories = super.saveByStep;
        s.stepSize = super.deltaStep;
        s.timeStep = super.deltaTime;
        s.initialSavingTime = super.initialSavingTime;
        s.differentFinalTimes = diffFinalTimes;
        s.unevenLengthTrajectories = unevenLength;
        s.names = super.trajectories.get(0).names;
        for (int k=0;k<runs;k++) {
            t = super.trajectories.get(k);
            data = t.data;
            for (int i=0;i<vars;i++)
                for (int j=0;j<t.savedPoints;j++)
                    s.data[i][j].add(data[i][j]);
            for (int i=0;i<vars;i++) {
                s.finalData[i].add(t.finalState[i]);
                s.finalSteps.add(t.totalSteps);
            }
        }
        s.initialized = true;
        s.computeStatistics();
        return s;
    }

}
